package com.lanou3g.demo.base.controller;

import com.lanou3g.demo.base.service.IServiceService;
import com.lanou3g.demo.entity.Account;
import com.lanou3g.demo.entity.Cost;
import com.lanou3g.demo.entity.Service;
import com.lanou3g.demo.entity.ServiceCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/serviceController")
public class ServiceController {

    @Autowired
    private IServiceService serviceService;


    //写responseBody似乎适用于ajax，携带参数到页面会出错
    @RequestMapping("selectAllDetail")
    public String service(HttpServletRequest request, ModelMap model) {

        Service service = serviceService.selectAllDetail(Integer.valueOf(request.getParameter("serviceId")));
        model.addAttribute("service", service);


        return "/service/service_detail";
    }

    @RequestMapping("modiDetail")
    public String modiDetail(HttpServletRequest request, ModelMap model) {

        Service service = serviceService.selectAllDetail(Integer.valueOf(request.getParameter("serviceId")));
        model.addAttribute("service", service);
//可以查不同语句，往model里添加多个数据，在前端同一个页面显示不同sql语句查出来的数据
        List<Cost> costList = serviceService.selectAllCost();
        model.addAttribute("costList", costList);

        return "/service/service_modi";
    }
//
//    @RequestMapping("/selectAll")
//    public String selectAll(ModelMap model) {
//
////        serviceService = new ServiceServiceImpl();
//        List<Service> serviceList = serviceService.selectAll();
//
//        model.addAttribute("serviceList", serviceList);
////        request.setAttribute("serviceList",serviceList);
//        return "/service/service_list";
//    }

    @RequestMapping("/add")
    public String add(Service service, ModelMap model) {

//        request不能注入，getParammeter不用，方法括号里就是前段传过来的对象，model是传给前端
        //的对象
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        service.setCreateDate(Timestamp.valueOf(format));
        serviceService.add(service);

        //重定向到controller 重新查数据 不会拼.jsp
        return "redirect:/serviceController/selectAll";
    }

    @RequestMapping("/account")
    @ResponseBody
    public Account hasAccount(@RequestBody Account account) {
        Account hasAccount = serviceService.hasAccount(account);
        System.out.println(hasAccount);
        return hasAccount;
    }

    @RequestMapping("/cost")
    @ResponseBody
    public List<Cost> costList() {
        List<Cost> costList = serviceService.selectAllCost();
        System.out.println(costList);
        return costList;
    }

    @RequestMapping("/start")
    @ResponseBody
    public String start(Service service, Model model) {
        serviceService.start(service);
        return "success";
    }

    @RequestMapping("/pause")
    @ResponseBody
    public String pause(Service service, Model model) {
        serviceService.pause(service);

//        List<Service> serviceList = serviceService.selectAll();
//        model.addAttribute("serviceList", serviceList);
        return "success";
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Service service, Model model) {
        serviceService.delete(service);
        //得对应名字 得包装类
//ajax传到controller，controller返回的数据识别返回页面 model，只能识别字符串数据
//        List<Service> serviceList = serviceService.selectAll();
//        model.addAttribute("serviceList", serviceList);
        return "success";
    }

    @RequestMapping("updateCost")
    public String updateCost(Service service) {
        serviceService.updateCost(service);
        return "redirect:/serviceController/findByPage";
        //起name才可以上传表单，设value才能得到它的值，不是id，下拉框设置id后进行选择检查id没变但
//        后台可以得到数据
    }

    @RequestMapping("findAllByCondition")
    public String findAllByCondition(ServiceCondition serviceCondition, Model model) {
        if (serviceCondition.getStatus() == null) {
            serviceCondition.setStatus("");
        }
        if (serviceCondition.getPage() == null || serviceCondition.getPage().toString().equals("0")) {
            serviceCondition.setPage(1);
        }
        if (serviceCondition.getOsUsername() != null) {
            model.addAttribute("OsUsername", serviceCondition.getOsUsername());
        }
        if (serviceCondition.getUnixHost() != null) {
            model.addAttribute("UnixHost", serviceCondition.getUnixHost());
        }
        if (serviceCondition.getIdcardNo() != null) {
            model.addAttribute("IdcardNo", serviceCondition.getIdcardNo());
        }

        // 获取当前页 非0情况下
        Integer currentPage = serviceCondition.getPage();
        // 获取状态 传给前端
        String status = serviceCondition.getStatus();
        model.addAttribute("status", status);
        // 设置页数 公式 (page-1) * 3
        serviceCondition.setPage((serviceCondition.getPage() - 1) * 5);
        List<ServiceCondition> serviceList = serviceService.findAllByCondition(serviceCondition);
        model.addAttribute("serviceList", serviceList);
        List<ServiceCondition> countByCondition = serviceService.findCountByCondition(serviceCondition);
        for (ServiceCondition condition : countByCondition) {
            Integer total = condition.getTotal();
            int totalPage = total / 5;
            if (total >= 0 && total < 5) {
                totalPage = 1;
            } else {
                if ((total % 5) >= 1) {
                    totalPage += 1;
                }
            }
            model.addAttribute("totalPage", totalPage);
            if (currentPage >= totalPage) {
                currentPage = totalPage - 1;
            }
            model.addAttribute("currentPage", currentPage);
        }
//        需要传一个对象，但是它的实体类中没有需要的属性，重建一个包含多个表的属性的单独
        //的一个实体类
        return "/service/service_list";
    }

    @RequestMapping("findByPage")
    public String findByPage(Model model, HttpServletRequest request) {
        // 查找所有数据 走Mapper
        List<Service> list = serviceService.selectAll();
        // 所有数据集合长度获取 做为数据总数
        int total = list.size();
        // 用数据总数除每个页面显示的条数 即共有多少页
        int totalPage = total / 5;
        if ((total %= 5) != 0) {
            totalPage += 1;
        }
        // 把总页数穿到前端作为循环长度
        model.addAttribute("totalPage", totalPage);
        // 首先判断是否有page值 如果没有默认加载第一页 如果有就拿到page的值
        if (request.getParameter("page") != null) {
            // 把page转换成数字 即当前页的数字
            Integer currentPage = Integer.valueOf(request.getParameter("page"));
            if (currentPage > 0 && currentPage <= totalPage) {
                // 根据当前页查询数据 公式就是 当前页-1 * 每页条数
                List<Service> accountServiceByPage = serviceService.findByPage((currentPage - 1) * 5);
                // 把数据集合传回前端遍历输出
                // 因为不同情况需要不同处理 所以写了很多分支 暂时想不到办法解决
                // 但总的来说应该就这几种情况了
                model.addAttribute("serviceList", accountServiceByPage);
                model.addAttribute("currentPage", currentPage);
            }
            // 当前页大于总页数 也就是在最后一页点下一页的傻子
            else if (currentPage >= totalPage) {
                List<Service> firstList = serviceService.findByPage((totalPage - 1) * 5);
                model.addAttribute("serviceList", firstList);
                model.addAttribute("currentPage", totalPage);
                return "/service/service_list";
            }
            // 同上 在第一页点上一页的傻子
            else if (currentPage <= 0) {
                List<Service> firstList = serviceService.findByPage(0);
                model.addAttribute("serviceList", firstList);
                model.addAttribute("currentPage", 1);
                return "/service/service_list";
            } else {
                List<Service> firstList = serviceService.findByPage(0);
                model.addAttribute("serviceList", firstList);
                model.addAttribute("currentPage", 0);
                return "/service/service_list";
            }
            return "/service/service_list";
        }
        // 没有page值 所以默认显示第一页 同上一个
        else {
            List<Service> firstList = serviceService.findByPage(0);
            model.addAttribute("serviceList", firstList);
            model.addAttribute("currentPage", 0);
            return "/service/service_list";
        }

    }


}
